using System;
using System.Text;
using System.Windows.Forms;
using CombatTracker.Creatures;

namespace CombatTracker.Controls
{
  public partial class AddMonsterDialog : Form
  {
    private readonly CreatureList _monsters;

    public CreatureList Monsters
    {
      get { return _monsters; }
    }

    public AddMonsterDialog()
    {
      InitializeComponent();
      _monsters = new CreatureList();
    }

    private void rdoManualInitiative_CheckedChanged(object sender, EventArgs e)
    {
      txtInitiativeRoll.Enabled = ((RadioButton) sender).Checked;
    }

    private void rdoSingleInitiative_CheckedChanged(object sender, EventArgs e)
    {
      if (((RadioButton) sender).Checked)
      {
        txtInitiativeRoll.Enabled = false;
      }
    }

    private void rdoGroupInitiative_CheckedChanged(object sender, EventArgs e)
    {
      if (((RadioButton) sender).Checked)
      {
        txtInitiativeRoll.Enabled = false;
      }
    }

    private void numQuantity_ValueChanged(object sender, EventArgs e)
    {
      rdoGroupInitiative.Enabled = ((NumericUpDown) sender).Value > 1;
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
      StringBuilder errorMessages = new StringBuilder(String.Empty);
      bool validForm = true;
      int bonus;
      int roll;

      if (txtName.Text == String.Empty)
      {
        validForm = false;
        errorMessages.AppendLine("- A name must be entered.");
      }

      if (!int.TryParse(txtInitiativeBonus.Text, out bonus))
      {
        validForm = false;
        errorMessages.AppendLine("- You must enter a numeric initiative bonus.");
      }

      if (rdoManualInitiative.Checked && !int.TryParse(txtInitiativeRoll.Text, out roll))
      {
        validForm = false;
        errorMessages.AppendLine("- If Manual Initiative is selected, you must enter a numeric initiative roll.");
      }

      if (rdoGroupInitiative.Checked && numQuantity.Value <= 1)
      {
        validForm = false;
        errorMessages.AppendLine("- If Group Initiative is selected, you must request more than 1 monster.");
      }

      if (validForm)
      {
        CreateMonsters();
        DialogResult = DialogResult.OK;
        Close();
      }
      else
      {
        MessageBox.Show("The following things must be fixed before you can 'Add': \r\n" + errorMessages, "Input Error",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation);
      }
    }

    private void CreateMonsters()
    {
      if (rdoManualInitiative.Checked)
      {
        for (int i = 1; i <= numQuantity.Value; i++)
        {
          Creature newMonster =
            CreatureFactory.CreateCreature(
              new CreatureParameters(txtName.Text + ((numQuantity.Value > 1) ? (" " + i.ToString()) : String.Empty),
                                     CreatureType.Monster));

          newMonster.InitiativeBonus = int.Parse(txtInitiativeBonus.Text);
          newMonster.InitiativeRoll = int.Parse(txtInitiativeRoll.Text);
          newMonster.InitiativeStatus = InitiativeState.InInitiative;

          _monsters.Add(newMonster);
        }
      }
      else if (rdoSingleInitiative.Checked)
      {
        for (int i = 1; i <= numQuantity.Value; i++)
        {
          Creature newMonster =
            CreatureFactory.CreateCreature(
              new CreatureParameters(txtName.Text + ((numQuantity.Value > 1) ? (" " + i.ToString()) : String.Empty),
                                     CreatureType.Monster));

          newMonster.InitiativeBonus = int.Parse(txtInitiativeBonus.Text);

          _monsters.Add(newMonster);
        }
      }
      else if (rdoGroupInitiative.Checked)
      {
        int groupInitiative = RollForInitiative(int.Parse(txtInitiativeBonus.Text));

        for (int i = 1; i <= numQuantity.Value; i++)
        {
          Creature newMonster =
            CreatureFactory.CreateCreature(
              new CreatureParameters(txtName.Text + ((numQuantity.Value > 1) ? (" " + i.ToString()) : String.Empty),
                                     CreatureType.Monster));

          newMonster.InitiativeBonus = int.Parse(txtInitiativeBonus.Text);
          newMonster.InitiativeRoll = groupInitiative;
          newMonster.InitiativeStatus = InitiativeState.InInitiative;

          _monsters.Add(newMonster);
        }
      }
    }

    private static int RollForInitiative(int bonus)
    {
      Random numberGenerator = new Random((int) DateTime.Now.Ticks);
      return numberGenerator.Next(1, 20) + bonus;
    }
  }
}